//
//  JYLoginController.m
//  JYFarm
//
//  Created by 花花 on 2017/10/10.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYLoginController.h"
#import "ChangeBuTitleTool.h"
#import "JYSetingPwdController.h"
#import "BaseTabBarViewController.h"
#import "JYRegisterController.h"
#import "AFNetworking.h"
#import "JYUserModel.h"

@interface JYLoginController (){

   NSString  *_titleText;
}

//账号
@property (weak, nonatomic) IBOutlet UITextField *accountTextField;
//密码或者验证码
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;

//获取验证码
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;

//提示内容
@property (weak, nonatomic) IBOutlet UILabel *showLabel;

//登录按钮
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@property (weak, nonatomic) IBOutlet UIImageView *imgView;

@property(assign, nonatomic)BOOL isPwdLogin;
@end

@implementation JYLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"登录";
    self.view.backgroundColor = [UIColor whiteColor];
 
    
}
- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    _isPwdLogin = NO;
}

#pragma mark - SEL Methods
//登录
- (IBAction)loginOnClick:(UIButton *)sender {
    
    if (self.accountTextField.text.length == 0) {
        
        [MBProgressHUD LY_ShowError:@"请输入账号" time: 2.0];
        return;
    }else if (self.pwdTextField.text.length == 0){
        
        
        [MBProgressHUD LY_ShowError:_isPwdLogin?@"请输入密码":@"请输入验证码" time: 2.0];
        return;
    }
    
    [self loginFormNetwork];

}
//获取验证码
- (IBAction)getCodeOnClick:(id)sender {
    
}
//新用户注册
- (IBAction)registerOnClick:(id)sender {
    
    [self.navigationController pushViewController:[JYRegisterController new] animated:YES];
}
//忘记密码
- (IBAction)forgetPwd:(id)sender {
    
    
    JYSetingPwdController *settingVc = [[JYSetingPwdController alloc]initWithIsSetingPwd:YES];
    [self.navigationController pushViewController:settingVc animated:YES];
}

//密码登录、短信验证码登录
- (IBAction)loginmanner:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"密码登录"]) {
        
        [ChangeBuTitleTool changeBuTitleWithText:^(NSString *titleText) { //短信验证码登录
            [sender setTitle:titleText forState:(UIControlStateNormal)];
            _getCodeBtn.hidden = YES;
            NSLog(@"%@-----",titleText);
            _isPwdLogin = YES;
            _pwdTextField.placeholder = @"请输入密码";
            _imgView.image = [UIImage imageNamed:@"login_verification_code_icon"];
            
            
        }];
    }else{
        
        [ChangeBuTitleTool changeBuTitleText:^(NSString *titleText) { //密码登录
            [sender setTitle:titleText forState:(UIControlStateNormal)];

            NSLog(@"%@---登录--",titleText);
            _isPwdLogin = NO;
            _getCodeBtn.hidden = NO;
            _pwdTextField.placeholder = @"请输入验证码";
            _imgView.image = [UIImage imageNamed:@"login_password"];
            
    
        }];

    
    }
    
    
}

- (void)loginFormNetwork{

    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userName"] = @"15202155393";
//    self.accountTextField.text;
//    @"15202155393";
    
    if (_isPwdLogin) {
       param[@"password"]= [LYTools MD5ForLower32Bate:self.pwdTextField.text];
    }else{
        param[@"smsCode"]= self.pwdTextField.text;

    }

    [HTTPRequest POST:kLoginUrl parameter:param success:^(id resposeObject) {
        
        NSLog(@"%@",resposeObject);
        
        if (SUCCESS) {
       
            JYUserModel *model = [JYUserModel mj_objectWithKeyValues:resposeObject[@"bean"]];
            //保存用户信息到本地
            [LH saveUserInfoToLocal:model];
            [LH loginSuccessDataHandle];
            //保存到内存中
            LH.userInfo = model;

            
            BaseTabBarViewController *tabBar = [[BaseTabBarViewController alloc]init];
            [self presentViewController:tabBar animated:YES completion:nil];
            
        }else{
            
            [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
        }
        
        
    } failure:^(NSError *error) {
        self.loginBtn.userInteractionEnabled = YES;
        [MBProgressHUD LY_ShowError:(kNoNetworkTips) time:2.0];
    }];
    
}

@end
